package com.fulu.game.bi.service.impl.play;

import com.fulu.game.bi.common.util.csv.CsvExportUtil;
import com.fulu.game.bi.common.util.csv.Mapper;
import com.fulu.game.bi.common.util.csv.Querier;
import com.fulu.game.bi.entity.bo.channel.GameCategoryListBO;
import com.fulu.game.bi.entity.enums.QueryTypeStatusEnum;
import com.fulu.game.bi.entity.vo.play.GameCategoryStatVO;
import com.fulu.game.bi.hbase.HBaseGameCategoryStatDAO;
import com.fulu.game.bi.redis.service.play.GameCategoryService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class GameCategoryServiceImpl extends BasePlayService implements GameCategoryService {
    @Autowired
    private HBaseGameCategoryStatDAO hBaseGameCategoryStatDAO;

    @Override
    public PageInfo<GameCategoryStatVO> list(GameCategoryListBO listBO) {
        listBO.setTableName(gameCategoryStat.get(listBO.getQueryType()));
        listBO.setPageNum((listBO.getPageNum() - 1) * listBO.getPageSize());
        List<GameCategoryStatVO> list = hBaseGameCategoryStatDAO.list(listBO);
        Integer count = hBaseGameCategoryStatDAO.count(listBO);
        list = list.stream().map(map -> {
            map.setQueryType(listBO.getQueryType());
            return map;
        }).collect(Collectors.toList());
        PageInfo<GameCategoryStatVO> pageInfo = new PageInfo<>(list);
        pageInfo.setTotal(count == null ? 0 : count);
        return pageInfo;
    }

    @Override
    public void export(GameCategoryListBO bo) {
        String fileName = "陪玩品类";
        LinkedHashMap<String, Mapper<GameCategoryStatVO>> map = new LinkedHashMap<>();
        QueryTypeStatusEnum anEnum = QueryTypeStatusEnum.getEnum(bo.getQueryType());
        switch (anEnum) {
            case DAY:
                map.put("日期", v -> v.getStartDate());
                break;
            default:
                map.put("日期", v -> v.getStartDate() + "-" + v.getEndDate());
                break;
        }
        map.put("品类", v -> v.getCategoryName());
        map.put("新增陪玩师", v -> v.getNewAddPlayeeNum() + "");
        map.put("活跃陪玩师", v -> v.getLivelyPlayeeNum() + "");
        map.put("接单陪玩师", v -> v.getOrderPlayeeNum() + "");
        map.put("退款陪玩师", v -> v.getRefundPlayeeNum() + "");
        map.put("下单人数", v -> v.getOrderUserNum() + "");
        map.put("下单次数", v -> v.getOrderNum() + "");
        map.put("下单金额", v -> v.getOrderMoneyAll().toString());
        map.put("下单时长", v -> v.getOrderDurationTime().toString());
        map.put("退款人数", v -> v.getRefundUserNum() + "");
        map.put("退款次数", v -> v.getRefundNum() + "");
        Querier querier = (num) -> list(bo);
        CsvExportUtil.export(100, fileName, map, querier);
    }

    @Override
    public List<String> category() {
        return hBaseGameCategoryStatDAO.category();
    }
}
